﻿Imports System.Net
Imports System.Data
Imports DotRas
Imports VPNClient.PassWord
Imports VPNClient.Dialer_VPN

Public Class VPNClient
    Public EntryName As String = "VPN连接"
    Public vpn_service_ip As String = ""
    Public vpn_cont_Mode As Boolean = 1
    Public vpn_cont_route As Boolean = 1
    Private connectionHandle As RasHandle

    Private Sub VPNClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.ImageLocation = "http://show.qidian80.net/images/AD726X90.gif"
        bar_time_now.Text = Now
        DataShow()
    End Sub

    Sub DataShow()
        Dim xmlPath As String = My.Application.Info.DirectoryPath & "\Config\authors.xml"       '定义xml路径
        AuthorsDataSet.ReadXml(xmlPath)       '读取xml文件
        DataGridView.DataSource = AuthorsDataSet       'Data绑定数据源
        DataGridView.DataMember = "SERVICE"       '设置显示数据

        '设置隐藏部分列
        DataGridView.Columns("id").Visible = False
        DataGridView.Columns("user_LV").Visible = False
        DataGridView.Columns("SERVICE_ip").Visible = False
        '设置宽度
        DataGridView.Columns("SERVICE_name").Width = 80     '总宽度428
        DataGridView.Columns("SERVICE_speed").Width = 80
        'DataGridView.Columns("SERVICE_desc").Width = 278
        '设置列标题名称、列居中显示
        DataGridView.Columns("SERVICE_name").HeaderText = "主机名称"
        DataGridView.Columns("SERVICE_name").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DataGridView.Columns("SERVICE_speed").HeaderText = "连接速度"
        DataGridView.Columns("SERVICE_speed").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DataGridView.Columns("SERVICE_desc").HeaderText = "服务器描述"
    End Sub

    Private Sub Timer_now_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        bar_time_now.Text = Now
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Process.Start("http://www.baidu.com")
    End Sub

    Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Cursor = Cursors.Hand
    End Sub

    Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub DataGridView_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView.MouseClick
        '  MessageBox.Show(DataGridView.CurrentCell.Value)          '取值
        '  MessageBox.Show(DataGridView.CurrentCell.ColumnIndex)    '取列编号
        '  MessageBox.Show(DataGridView.CurrentCell.RowIndex)       '取行编号

        Dim PingReply As System.Net.NetworkInformation.PingReply
        Dim Ping As New System.Net.NetworkInformation.Ping
        PingReply = Ping.Send(DataGridView.Rows(DataGridView.CurrentCell.RowIndex).Cells(5).Value)
        If PingReply.Status = Net.NetworkInformation.IPStatus.Success Then
            DataGridView.Rows(DataGridView.CurrentCell.RowIndex).Cells(3).Value = PingReply.RoundtripTime.ToString & "ms"
        Else
            DataGridView.Rows(DataGridView.CurrentCell.RowIndex).Cells(3).Value = "连接失败"
        End If

        EntryName = DataGridView.Rows(DataGridView.CurrentCell.RowIndex).Cells(4).Value
        vpn_service_ip = DataGridView.Rows(DataGridView.CurrentCell.RowIndex).Cells(5).Value
        
    End Sub

    Private Sub btn_Text_Speed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '  Dim PingReply As System.Net.NetworkInformation.PingReply
        '  Dim Ping As New System.Net.NetworkInformation.Ping
        '  Dim i As Integer
        '  ' PingReply = Ping.Send("imzhou.com")
        '  ' TextBox3.Text = PingReply.Address.ToString & "---" & PingReply.RoundtripTime.ToString & "---" & PingReply.Status.ToString
        '
        '  For i = 0 To DataGridView.RowCount - 1
        '      PingReply = Ping.Send(DataGridView.Rows(i).Cells(5).Value)
        '
        '      If PingReply.Status = Net.NetworkInformation.IPStatus.Success Then
        '          DataGridView.Rows(i).Cells(3).Value = PingReply.RoundtripTime.ToString & "ms"
        '      Else
        '          DataGridView.Rows(i).Cells(3).Value = "失败"
        '      End If
        '     
        '  Next i
        '
    End Sub

    Private Sub btn_Connection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Connection.Click
        '拨号
        VPN_CALL(EntryName, vpn_service_ip, vpn_cont_Mode, vpn_cont_route, TextBox_username.Text, TextBox_pwd.Text)

    End Sub



    Private Sub btn_Connection_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Connection_Cancel.Click
        If (Me.Dialer.IsBusy) Then
            Me.Dialer.DialAsyncCancel()
        Else
            '已完成的连接尝试，试图找到在活动连接的连接。
            Dim connection As RasConnection = RasConnection.GetActiveConnectionByHandle(Me.connectionHandle)
            If (connection IsNot Nothing) Then
                '发现连接已断开。
                connection.HangUp()
            End If
        End If
    End Sub

    Private Sub Rad_L2TP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rad_L2TP.Click
        vpn_cont_Mode = False
    End Sub

    Private Sub Rad_PPTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rad_PPTP.Click
        vpn_cont_Mode = True
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Text = "全局加速" Then
            vpn_cont_route = True
            Label_route.Text = "所有流量走VPN"
        ElseIf ComboBox1.Text = "智能路由" Then
            vpn_cont_route = False
            Label_route.Text = "国外流量走VPN"
        End If
    End Sub

    Sub Route_Add()
        Process.Start(My.Application.Info.DirectoryPath & "\test.bat ")
    End Sub

    Private Sub btn_Buy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Buy.Click
        TextBox3.Text = Encode(TextBox_pwd.Text)
    End Sub

    Private Sub btn_Help_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Help.Click
        TextBox_username.Text = Decode(TextBox3.Text)
    End Sub
End Class
